var attnStatistics = {
    events : {
        ".attnStatistics-body .detail click" : "getDetail",
        ".attnStatistics-body .search-record-list click" : "getStatisRecord",
        ".attnStatistics-body .record-export click" : "recordExport",
        
        
    },
	init : function(){
		var _this = this;
        document.title = "考勤设置 - 考勤管理";
        var temp = Util.template($("#attnStatistics-template").html(),{});
        $(".commonBody-rightBody").html(temp);
        Util.event.call(_this,_this.events)
        $(".date-from").datetimepicker({
            format: 'yyyy-mm-dd',
            autoclose: true,
            language: "zh-CN",
            pickerPosition: "bottom-left",
            minView:2
        });
        this.initTableCopy();
        this.getDeptList();
	},
    getDeptList : function(){
        var that = this;
        Util.ajaxRequest({
            type: "get",
            url: interfaceURL.getHolidayDeptList(),
            dataType: "json",
            showLoading : false,
            success : function(data){
                if(data.code == 200){
                    $.each(data.result,function(i,v){
                        $("#depId").append('<option value="'+v.id+'">'+v.name+'</option>');
                    })
                    that.getStatisRecord();
                }
            }
        })
    },
    getStatisRecord : function(){
        var obj = Util.getData($(".attnbox-top")),that = this;
        obj.startDate = obj.startDate != "" ? obj.startDate + " 00:00:00" : "";
        obj.endDate = obj.endDate != "" ? obj.endDate + " 23:59:59" : "";
        Util.ajaxRequest({
            type: "get",
            url: interfaceURL.statisRecord(),
            dataType: "json",
            data : obj,
            success : function(data){
                if(data.code == 200){
                    that.showTable(data.result);
                }
            }
        })
    },
    showTable : function(data){
        $.each(data,function(i,v){
            v.operation = '<button class="btn btn-blue detail">明细</button>';
            v.lateNum = v.lateNum > 0 ?'<div class="alarm-red">' + v.lateNum + '</div>' :v.lateNum;
            v.leaveEarlyNum = v.leaveEarlyNum > 0 ?'<div class="alarm-blue">' + v.leaveEarlyNum + '</div>' :v.leaveEarlyNum;
            v.absenceNum = v.absenceNum > 0 ?'<div class="alarm-yellow">' + v.absenceNum + '</div>' :v.absenceNum;
        })
        $(".statistics-list").cakeTable({
            head: [
                {field: "userNum",name: "工号"},
                {field: "realName",name: "姓名"},
                {field: "depName",name: "部门",size:12},
                {field: "lateNum",name: "迟到次数"},
                {field: "leaveEarlyNum",name: "早退次数"},
                {field: "absenceNum",name: "未签卡次数"},
                {field: "operation",name: "操作",type:2}
            ],
            rowData : [{key:"userId",attrName:"data-id"}],
            sort: false,
            columns: data,
            showPage:false
        })
    },
    getDetail : function(ele){
        var userId = $(ele).parents("tr").attr("data-id"),
            that = this;

        Util.ajaxRequest({
            type: "get",
            url: interfaceURL.statisRecordDetail(),
            dataType: "json",
            data : {userId : userId},
            success : function(data){
                if(data.code == 200){
                    that.showDetail(data.result,ele)
                }
            }
        })
    },
    showDetail : function(data,ele){
        $.each(data,function(i,v){
            v.checkType = v.checkType == 1 ? "正常" : v.checkType == 2 ? "迟到" : v.checkType == 3 ? "早退" : v.checkType == 4 ? "未打卡"  : "迟到早退";
        })
        var idNum = $(ele).parents("tr").children().eq(1).html();
        var name = $(ele).parents("tr").children().eq(2).html();
        var department = $(ele).parents("tr").children().eq(3).html();
        pop.dialog({
            popTitle: "考勤明细",
            popContent: Util.template($("#statistics-detail-template").html(), {idNum:idNum,name:name,department:department}),
            width :　"595px",
            maxHeight : "500px",
            popOkText : "关闭",
            popCancelShow : false,
            onLoad: function () {
                $(".statistics-detail-list").cakeTable({
                    head: [
                        {field: "checkType",name: "类型"},
                        {field: "recordDate",name: "日期",format:"date"},
                        {field: "checkIn",name: "签到时间",format:"hour"},
                        {field: "checkOut",name: "签退时间",format:"hour"}
                    ],
                    sort: false,
                    columns: data,
                    showPage:false
                })
            }
        })
    },
    initTableCopy: function () {
        var that = this;
        $(".radioCheck").click(function(){
            if ($(this).find(".checked").length < 1) {
                $(".radioCheck .user-radio-z").removeClass("checked");
                $(this).find(".user-radio-z").addClass("checked");
                var type = $(this).find(".user-radio-z").attr("data-type"),start = "",end = "";
                if(type == "0"){start = new Date().format("yyyy-MM-dd");end = new Date().format("yyyy-MM-dd");}
                if(type == "1"){
                    start = new Date(that.getWeekStartDate()).format("yyyy-MM-dd");
                    end = new Date(that.getWeekEndDate()).format("yyyy-MM-dd");
                }
                if(type == "2"){
                    start = new Date(that.getMonthStartDate()).format("yyyy-MM-dd");
                    end = new Date(that.getMonthEndDate()).format("yyyy-MM-dd");
                }
                $("#startDate").val(start);
                $("#endDate").val(end);
            }
        })
    },
    getCur : function(){
        return{
            nowDayOfWeek : new Date().getDay(), //今天本周的第几天 
            nowDay : new Date().getDate(), //当前日 
            nowMonth : new Date().getMonth(), //当前月 
            nowYear : new Date().getFullYear() //当前年 
        }
    },
    getWeekStartDate : function() {
        var cur =  this.getCur();
        var weekStartDate = new Date(cur.nowYear, cur.nowMonth, cur.nowDay - cur.nowDayOfWeek + 1); 
        return weekStartDate; 
    },
    getWeekEndDate : function() { 
        var cur =  this.getCur();
        var weekEndDate = new Date(cur.nowYear, cur.nowMonth, cur.nowDay + (7 - cur.nowDayOfWeek)); 
        return weekEndDate; 
    },
    getMonthStartDate : function(){
        var cur =  this.getCur();
        var monthStartDate = new Date(cur.nowYear, cur.nowMonth, 1); 
        return monthStartDate; 
    },
    getMonthEndDate : function(){
    var cur =  this.getCur(); 
        var monthEndDate = new Date(cur.nowYear, cur.nowMonth, this.getMonthDays(cur.nowMonth)); 
        return monthEndDate; 
    },
    getMonthDays : function(myMonth){ 
        var cur =  this.getCur();
        var monthStartDate = new Date(cur.nowYear, myMonth, 1); 
        var monthEndDate = new Date(cur.nowYear, myMonth + 1, 1); 
        var days = (monthEndDate - monthStartDate)/(1000 * 60 * 60 * 24); 
        return days; 
    },
    recordExport : function(){
        var obj = [];
        obj.push("realName="+$("#realName").val());
        obj.push("userNum="+$("#userNum").val());
        obj.push("depId="+$("#depId").val());
        obj.push("startDate=" + ($("#startDate").val() == "" ? "" : $("#startDate").val() + " 00:00:00"));
        obj.push("endDate=" + ($("#endDate").val() == "" ? "" : $("#endDate").val() + " 23:59:59"));
        window.open(interfaceURL.exportStatis() + "?" + obj.join("&"));
    }
};